package com.chunfeng.mypen.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chunfeng.mypen.dao.RoleDao;
import com.chunfeng.mypen.entity.Permission;
import com.chunfeng.mypen.entity.Role;
import com.chunfeng.mypen.properties.SystemProperties;
import com.chunfeng.mypen.result.JsonRequest;
import com.chunfeng.mypen.result.exenum.RequestException;
import com.chunfeng.mypen.service.PermissionService;
import com.chunfeng.mypen.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 角色业务层实现
 *
 * @author by 春风能解释
 * <p>
 * 2023/11/15
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao, Role> implements RoleService {
    /**
     * 角色数据层注入
     */
    @Autowired
    private RoleDao roleDao;
    /**
     * 权限业务层注入
     */
    @Autowired
    private PermissionService permissionService;
    /**
     * 系统配置
     */
    @Autowired
    private SystemProperties systemProperties;

    /**
     * 条件查询角色
     *
     * @param current 当前页码
     * @param size    每页长度
     * @param role    条件
     * @return JSON
     */
    @Override
    public JsonRequest<List<Role>> lookRole(Integer current, Integer size, Role role) {
        //查询
        Page<Role> rolePage = roleDao.selectAllRole(new Page<>(current, size), role);
        List<Role> roles = rolePage.getRecords();
        if (roles.isEmpty()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        Map<String, Object> pages = new HashMap<>();
        //记录总页数
        pages.put(systemProperties.getPageKeyName(), rolePage.getTotal());
        return JsonRequest.success(roles, pages);
    }

    /**
     * 查询单个角色信息
     *
     * @param id ID
     * @return JSON
     */
    @Override
    public JsonRequest<Role> lookOneRole(String id) {
        Role role = roleDao.selectById(id);
        if (Objects.isNull(role)) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        JsonRequest<List<Permission>> jsonRequest = permissionService.lookPermissionOfOneRole(role.getId());
        if (!jsonRequest.getSuccess()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        //加入参数
        role.setPermissionList(jsonRequest.getData());
        return JsonRequest.success(role);
    }


    /**
     * 添加一条角色信息
     *
     * @param role 待添加的角色
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> addOneRole(Role role) {
        //添加
        int column = roleDao.insert(role);
        //判断是否成功
        if (column < 1) {
            return JsonRequest.error(RequestException.INSERT_ERROR);
        }
        return JsonRequest.success(column);
    }

    /**
     * 修改一条角色信息
     *
     * @param role 待修改的角色信息
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> updateOneRole(Role role) {
        //查询
        Role role1 = roleDao.selectById(role.getId());
        //判断用户是否存在
        if (Objects.isNull(role1)) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        //修改
        int column = roleDao.updateById(role);
        if (column < 1) {
            return JsonRequest.error(RequestException.UPDATE_ERROR);
        }
        return JsonRequest.success(column);
    }

    /**
     * 批量删除角色
     *
     * @param ids ID
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> deleteRole(List<String> ids) {
        //查询符合ID的信息
        Integer size = roleDao.selectCountById(ids);
        //当角色ID数与查出的存在差异时,我们认为已有数据被删除
        if (size != ids.size()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        //删除
        int column = roleDao.deleteBatchIds(ids);
        if (column < 1) {
            return JsonRequest.error(RequestException.DELETE_ERROR);
        }
        return JsonRequest.success(column);
    }
}
